package com.qf.admin.controller;



import com.qf.admin.entity.AdminUser;
import com.qf.admin.model.Results;
import com.qf.admin.service.ILoginAdmin;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/loginAdmin")
public class LoginAdminController {
    private ILoginAdmin loginAdmin;

    public LoginAdminController(ILoginAdmin loginAdminImpl) {
        this.loginAdmin = loginAdminImpl;
    }

    @GetMapping("/{username}/{password}")
    public Results checkUser(@PathVariable("username") String username, @PathVariable("password") String password
                       , HttpServletRequest req) {
        System.out.println("进入了登录验证");

        Results results = new Results();
        System.out.println(DigestUtils.md5Hex(password));

        AdminUser adminUser = loginAdmin.checkUser(username, DigestUtils.md5Hex(password));

        if (null !=adminUser){
            req.getSession().setAttribute("adminUser", adminUser);
            results.setCode(1);
            results.setMsg("success");
        }else{
            results.setCode(-1);
            results.setMsg("fail");
        }
        return results;
    }
}
